from django.test import TestCase
from django.shortcuts import  HttpResponse
from django.template.loader import  get_template
from django.views.decorators.csrf import csrf_exempt
import json
import requests
import datetime,time
import urllib3
# Create your tests here.

def test_str_to_date(request):
    d1="2019-06-04"
    start =datetime.datetime.strptime(d1,'%Y-%c-%e')
    str2 = start
    print(str2)

def show_html(request):
    template = get_template('postdata.html')
    html = template.render(locals())
    return HttpResponse(html)

#post_kalf_order
def post_kalf_order(request):
    pass

@csrf_exempt
def post_kalf(request):
    html_url = "http://apit.icibei.com/order-service/order/external/acceptReceiptItems"
    data={
        'deliverCode': 'CS123456789',
        'deliverOrgCode': '7001211',
        'deliverOrgName': '北京康安利丰农业有限公司',
        'deliverTime': '2019-06-25 10:10:10',
        'detailDtoList': [
            {
                'deliverPrice': '1',
                'deliverQty': '1',
                'delverAmount': '1',
                'orderCode': 'PO300051906190004',
                'productCode': '11100762',
                'productName': '猪五花肉片40.4(25kg)',
                'spareDetailField1': '',
                'spareDetailField2': '',
                'spareDetailField3': '',
                'spareDetailField4': ''
            },
            {
                'deliverPrice': '1',
                'deliverQty': '1',
                'delverAmount': '1',
                'orderCode': 'PO300051906190004',
                'productCode': '11200022',
                'productName': '圣迪乐村鸡蛋',
                'spareDetailField1': '',
                'spareDetailField2': '',
                'spareDetailField3': '',
                'spareDetailField4': ''
            },
            {
                'deliverPrice': '1',
                'deliverQty': '1',
                'delverAmount': '1',
                'orderCode': 'PO300051906190004',
                'productCode': '11300702',
                'productName': '新康西红柿块',
                'spareDetailField1': '',
                'spareDetailField2': '',
                'spareDetailField3': '',
                'spareDetailField4': ''
            }
          ],
        'retailCode': '30005',
        'spareField1': '',
        'spareField2': ''
    }
    headers = {
        'Request Method':'POST',
        'Content-Type': 'application/json;charset=UTF-8',
        'x-auth-token': 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxOTkzMiIsImV4cCI6MTU2NDAzMjc1NCwiaWF0IjoxNTYxNDQwNzU0fQ.qcD7zYCMg6Oho7tHz1lKzAjR1qNGHz6X_VNlIi7GzF8',
        'Connection': 'keep-alive',
        'Host': 'apit.icibei.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
    }


    #http = urllib3.PoolManager()
    #encode_data = json.dumps(data).encode()
    #print(encode_data)

    #rr = http.request("GET",html_url,encode_data,headers=headers)
    #response = rr.data.decode()
    #print(response)

    #response = requests.request("POST",url=html_url,data=json.dumps(data),headers=headers).text
    response = requests.post(html_url,data=data,json=None,headers= headers).text
    #print(response)
    #varrrs = requests.request("POST",html_url,data)
    return HttpResponse(response)
